package com.game.primitive.common;

import com.game.primitive.context.PrimitivePlayer;
import org.apache.log4j.Logger;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;

public class PrimitiveMongoUtil extends Thread {
	private static Logger logger = Logger.getLogger(PrimitiveMongoUtil.class);
	private static BlockingQueue<Runnable> blockingQueue = new LinkedBlockingDeque<>();
	private static Executor executor = Executors.newSingleThreadExecutor();

	public static void incPlayerCoin(final PrimitivePlayer player, final long value) {
		try {
		} catch (Exception e) {
		}
	}

	/**
	 * 保存数据
	 *
	 * @param player
	 * @param value
	 * @param isAsyn 是否异步
	 */
	public static void incPlayerCoin(final PrimitivePlayer player, final long value, boolean isAsyn) {
		if (isAsyn) {
			try {
				blockingQueue.put(new Runnable() {
					public void run() {
						incPlayerCoin(player, value);
					}
				});
			} catch (InterruptedException e) {
				logger.error("保存数据", e);
			}
		} else {
			incPlayerCoin(player, value);
		}
	}

	@Override
	public void run() {
		while (true) {
			try {
				Runnable runnable = blockingQueue.take();
				executor.execute(runnable);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
